/********************************************/
  // Creation History
  // --------------------------------------------------------------
  //	Company:			HTHK
  //	NAME:				StaffProjectServlet.java
  //    Title   :			operate A Project Record such as:Create,Update,Delete
  //	FUNCTION:
  //	BY:				Poly Li
  //	DATE:				12/01/2004
  // --------------------------------------------------------------
/*********************************************/
// Copyright (c) 2002 HTHK

package tss.multiproject;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.sql.*;

import com.ffpc.sql.*;
import com.ffpc.struts.StrutsMessage;
import com.util.JdbcUtil;

import tss.common.DBServlet2;


public class StaffProjectServlet
  extends DBServlet2 {

  @Override
protected String perform(  HttpServletRequest request,
                             HttpServletResponse response,
                             StrutsMessage message)
    throws  ServletException,
            IOException,
            SQLException,
            DataException,
            Exception
  {
	Statement statement = null;
	ResultSet resultSet = null;
	statement = this.getConnection().createStatement();
     
     //New A Form Action
     String action = new String(request.getParameter("formAction"));

     //New A StaffProject Record
     StaffProjectBean staffProject = StaffProjectProcess.createBean();

     //Get A StaffProject Record
	staffProject.setProject_name(request.getParameter("project_name"));
	staffProject.setStaff_no(request.getParameter("staff_name"));
	String staff_name[] = null;
	if(request.getParameterValues("staff_name") != null)
		staff_name = request.getParameterValues("staff_name");
	String project_name = staffProject.getProject_name();
     //Insert
      if (action.trim().equalsIgnoreCase("Create"))
      {
			StaffProjectBean lStaffProject = StaffProjectProcess.createBean();
			boolean newFlag = false;
			for(int m=0;m<staff_name.length;m++)
			{
				lStaffProject.setProject_name(project_name);
				lStaffProject.setStaff_no(staff_name[m]);
				StaffProjectBean[] lStaffProjects = StaffProjectProcess.find(this.getConnection(),lStaffProject);
				if (lStaffProjects != null && lStaffProjects.length != 0)
					continue;
				if(StaffProjectProcess.insert(this.getConnection(),lStaffProject) > 0)
					newFlag = true;
			}
	            if ( newFlag )
	            	message.setMessage("Save successfully.");
	            else
	            	message.setMessage("Save failure.");
      }
      //Update
      else if (action.trim().equalsIgnoreCase("Update"))
      {
		//for delete all exist record
		
		StaffProjectBean staffProjectUpdate = StaffProjectProcess.createBean();
		staffProjectUpdate.setProject_name(staffProject.getProject_name());

		boolean delFlag = false;
		
		try
		{
			String del_staff_record = "delete from tss.tss_c_staff_project where project_name='"+project_name+"'";
			statement.executeUpdate(del_staff_record);
			delFlag = true;
		}
		catch(Exception eee)
		{
			message.setMessage("Update failure.");
		}finally{
			JdbcUtil.release(statement, resultSet);
		}
		
		//for insert new record
		//if(StaffProjectProcess.delete(this.getConnection(),staffProjectUpdate)>0)
		if(delFlag)
		{
			try
			{
				for(int m=0;m<staff_name.length;m++)
				{
					staffProjectUpdate.setStaff_no(staff_name[m]);
					StaffProjectProcess.insert(this.getConnection(),staffProjectUpdate);
				}
				message.setMessage("Save successfully.");
			}
			catch(Exception ee)
			{
				message.setMessage("Update failure.");
			}
		}
		else
			message.setMessage("Save failure.");
	}
      //Delete
	else if (action.trim().equalsIgnoreCase("Delete"))
	{
      		try
      		{
	      		for(int m=0;m<staff_name.length;m++)
	      		{
		      		staffProject.setStaff_no(staff_name[m]);
		      		StaffProjectProcess.delete(this.getConnection(),staffProject);
			}
			message.setMessage("Delete successfully.");
		}
		catch(Exception ee)
		{
			message.setMessage("Delete failure.");	
		}finally{
			JdbcUtil.release(statement, resultSet);
		}
	}
	else
	{
		message.setMessage("No action mapped!");
		if(statement!=null){
			JdbcUtil.release(statement, resultSet);
		}
	}
      return "tss/multiproject/MultiProjectSearch.jsp";

  }
}


